package com.nand.module.function.ui;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.Callback;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoBuilder;
import com.nand.library.base.http.OkGoUtils;
import com.nand.module.function.R;
import com.nand.module.function.bean.EqDatailBean;
import com.nand.module.function.bean.EqRepairDetailsBean;
import com.nand.module.function.constant.URL;
import com.nand.module.function.utils.LogUtils;
import com.nand.module.function.view.ImageActivity;

import java.util.ArrayList;
import java.util.List;

public class RepairDetalisActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvSbbm;
    private TextView mTvSbmc;
    private TextView mTvSybm;
    private TextView mTvGys;
    private TextView mTvGysdh;
    private TextView mTvGzlb;
    private TextView mTvGzdj;
    private TextView mTvGzms;
    private TextView mTvWxr;
    private TextView mTvWxfy;
    private TextView mTvClfs;
    private ImageView mIvImage2;
    private ImageView mIvImage1;
    private ImageView mIvImage3;
    private int reID;
    private List<String> imgList;
    private TextView mIvEmpty;

    @Override
    protected int initLayout() {
        return R.layout.activity_repair_detalis;
    }

    @Override
    protected void initView() {
        reID = getIntent().getIntExtra("reID", 1);
        mTvSbbm = findViewById(R.id.tv_sbbm);
        mTvSbmc = findViewById(R.id.tv_sbmc);
        mTvSybm = findViewById(R.id.tv_sybm);
        mTvGys = findViewById(R.id.tv_gys);
        mTvGysdh = findViewById(R.id.tv_gysdh);
        mTvGzlb = findViewById(R.id.tv_gzlb);
        mTvGzdj = findViewById(R.id.tv_gzdj);
        mTvGzms = findViewById(R.id.tv_gzms);
        mTvWxr = findViewById(R.id.tv_wxr);
        mTvWxfy = findViewById(R.id.tv_wxfy);
        mTvClfs = findViewById(R.id.tv_clfs);
        mIvImage1 = findViewById(R.id.iv_image1);
        mIvImage2 = findViewById(R.id.iv_image2);
        mIvImage3 = findViewById(R.id.iv_image3);
        mIvEmpty = findViewById(R.id.iv_empty);
        mIvImage1.setOnClickListener(this);
        mIvImage2.setOnClickListener(this);
        mIvImage3.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        HttpParams params = new HttpParams();
        params.put("reId", reID);


        OkGoUtils.getInstance().post(this, URL.EQREPAIR_DETAILS, params, 1, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                if (response.getCode() == 200) {
                    String data = response.getDataInfo().toString();

                    LogUtils.e("data========"+data);
                    //TODO 判断是否为空

                    if ("".equals(data)) {

                    } else {
                        EqRepairDetailsBean bean = JSON.parseObject(data, EqRepairDetailsBean.class);
                        mTvSbbm.setText(bean.getEquipId());
                        mTvSbmc.setText(bean.getEquipName());
                        mTvSybm.setText(bean.getDepartName());
                        mTvGys.setText(bean.getSupplierName());
                        mTvGysdh.setText(bean.getTelfo());
                        mTvGzlb.setText(bean.getHitchType());
                        mTvGzdj.setText(bean.getHitchGraContent());
                        mTvGzms.setText(bean.getHitchDetail());
                        mTvWxr.setText(bean.getUserName());
                        mTvWxfy.setText(bean.getRepearCost() + "");
                        mTvClfs.setText(bean.getDealMeth());

                        imgList = bean.getImgList();

                        switch (imgList.size()) {
                            case 0:
                                mIvEmpty.setVisibility(View.VISIBLE);
                                mIvImage1.setVisibility(View.GONE);
                                mIvImage2.setVisibility(View.GONE);
                                mIvImage3.setVisibility(View.GONE);
                                break;
                            case 1:
                                mIvImage2.setVisibility(View.GONE);
                                mIvImage3.setVisibility(View.GONE);
                                Glide.with(mContext).asBitmap().load(imgList.get(0)).into(mIvImage1);
                                break;
                            case 2:
                                mIvImage3.setVisibility(View.GONE);
                                Glide.with(mContext).asBitmap().load(imgList.get(0)).into(mIvImage1);
                                Glide.with(mContext).asBitmap().load(imgList.get(1)).into(mIvImage2);
                                break;
                            case 3:
                                Glide.with(mContext).asBitmap().load(imgList.get(0)).into(mIvImage1);
                                Glide.with(mContext).asBitmap().load(imgList.get(1)).into(mIvImage2);
                                Glide.with(mContext).asBitmap().load(imgList.get(2)).into(mIvImage3);
                                break;

                        }
                    }
                } else {
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onError(Throwable e, int id) {

            }
        });

    }

    @Override
    public void onClick(View view) {

        int id = view.getId();
        if (id == R.id.iv_image1) {
imageShow();
        } else if (id == R.id.iv_image2) {
            imageShow();
        } else if (id == R.id.iv_image3) {
            imageShow();
        }

    }

    private void imageShow() {
        ArrayList<String> strings = new ArrayList<>();
        switch (imgList.size()){
            case 1:
                strings.add(0, imgList.get(0));
                Intent intent1 = new Intent(mContext, ImageActivity.class);
                //intent1.putExtra(ImageActivity.CURRENT_IMG_URL, imgList.get(0));
                intent1.putStringArrayListExtra(ImageActivity.IMG_LIST, strings);
                startActivity(intent1);
                break;
            case 2:
                strings.add(0, imgList.get(0));
                strings.add(0, imgList.get(1));
                Intent intent2 = new Intent(mContext, ImageActivity.class);
                //intent2.putExtra(ImageActivity.CURRENT_IMG_URL, imgList.get(0));
                intent2.putStringArrayListExtra(ImageActivity.IMG_LIST, strings);
                startActivity(intent2);
                break;
            case 3:
                strings.add(0, imgList.get(0));
                strings.add(0, imgList.get(1));
                strings.add(0, imgList.get(2));
                Intent intent3 = new Intent(mContext, ImageActivity.class);
                //intent2.putExtra(ImageActivity.CURRENT_IMG_URL, imgList.get(0));
                intent3.putStringArrayListExtra(ImageActivity.IMG_LIST, strings);
                startActivity(intent3);
                break;
        }
    }
}
